import json
from datetime import datetime
from typing import Optional

from pydantic import Field, model_validator, HttpUrl

from common.dataclasses import SPLSection
from common.enums import StatusType
from common.schema import SchemaBase


# 基础的 Plugin 信息结构
class PluginSchemaBase(SchemaBase):
    name: str
    description: str
    cover_image: str
    status: int = Field(default=StatusType.enable.value)


# 创建 Plugin 参数
class CreatePluginParam(PluginSchemaBase):
    user_uuid: str | None = None
    server_url: str | None = str
    header_info: str | None = None
    return_info: str | None = None
    api_parameter: str | None = None

    @model_validator(mode='before')
    def check(cls, values):
        header_info = values.get('header_info', {})
        if header_info is not None:
            values['header_info'] = json.dumps(header_info)

        return_info = values.get('return_info', {})
        if return_info is not None:
            values['return_info'] = json.dumps(return_info)  # 设置默认值为空数组

        api_parameter = values.get('api_parameter', {})
        if api_parameter is not None:
            values['api_parameter'] = json.dumps(api_parameter)

        return values


# 更新 Plugin 参数
class UpdatePluginParam(PluginSchemaBase):
    name: str | None = None
    description: str | None = None
    cover_image: str | None = None
    status: int | None = None
    server_url: str | None = None
    header_info: str | None = None
    return_info: str | None = None
    api_parameter: str | None = None

    @model_validator(mode='before')
    def check(cls, values):
        header_info = values.get('header_info')
        if header_info is not None:
            values['header_info'] = json.dumps(header_info)

        return_info = values.get('return_info')
        if return_info is not None:
            values['return_info'] = json.dumps(return_info)  # 设置默认值为空数组

        api_parameter = values.get('api_parameter')
        if api_parameter is not None:
            values['api_parameter'] = json.dumps(api_parameter)

        return values


# Plugin 详情信息结构
class PluginDetailSchema(PluginSchemaBase):
    id: int
    uuid: str
    user_uuid: str

    server_url: str | None = None
    header_info: str | dict | None = None
    return_info: str | dict | None = None
    api_parameter: str | dict | None = None

    created_time: datetime
    updated_time: datetime | None = None

    @model_validator(mode='after')
    def handle(self):
        header_info = self.header_info
        if header_info:
            self.header_info = json.loads(header_info)
        else:
            self.header_info = {}

        return_info = self.return_info
        if return_info:
            self.return_info = json.loads(return_info)
        else:
            self.return_info = {}

        api_parameter = self.api_parameter
        if api_parameter:
            self.api_parameter = json.loads(api_parameter)
        else:
            self.api_parameter = {}

        return self


# Plugin 列表信息结构
class PluginListSchema(PluginSchemaBase):
    id: int
    uuid: str
    user_uuid: str

    created_time: datetime
    updated_time: datetime | None = None


# 当前 Plugin 信息详情
class GetPluginDetail(PluginDetailSchema):
    pass


# 当前 Plugin 信息详情
class GetPluginList(PluginListSchema):
    pass


class ImageRequest(SchemaBase):
    image_url: Optional[str] = None  # 图片 URL
    base64_image: Optional[str] = None  # Base64 编码的图片数据


class MarkdownConvertRequest(SchemaBase):
    content: str
